package com.xy.p2p.action.front;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.p2p.model.Tmember;
import com.xy.p2p.model.Tmemberassetaccount;
import com.xy.p2p.model.Tmembercredit;
import com.xy.p2p.service.TmembercreditService;



@Controller
public class TmembercreditAction{
	private static final Logger log = LoggerFactory.getLogger(TmembercreditAction.class);
	public TmembercreditAction() {
	}
 
 
 	/**
	 * 会员信用信息表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmembercreditAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tmembercredit", new Tmembercredit());
        setDict(model);
		return "tmembercreditEdit";
	}

	/**
	 * 会员信用信息表新增保存
	 * 
	 * @param tmembercredit
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tmembercreditSave.do")
	public String save(Tmembercredit tmembercredit, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tmembercredit, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tmembercredit = tmembercreditService.save(tmembercredit);
		return "redirect:v_tmembercreditList.do";
	}

	/**
	 * 会员信用信息表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tmembercreditDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tmembercreditService.remove(id);
			}
		}
		return "redirect:v_tmembercreditList.do";
	}

	/**
	 * 会员信用信息表编辑页面
	 * 
	 * @param mcid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmembercreditEdit.do")
	public String edit(long mcid, HttpServletRequest request, ModelMap model) {
		Tmembercredit tmembercredit = tmembercreditService.get(mcid);
		model.addAttribute("tmembercredit", tmembercredit);
        setDict(model);
		return "tmembercreditEdit";
	}

	/**
	 * 会员信用信息表详情
	 * 
	 * @param mcid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmembercreditDetail.do")
	public String detail(long mcid, HttpServletRequest request, ModelMap model) {
		Tmembercredit tmembercredit = tmembercreditService.get(mcid);
		model.addAttribute("tmembercredit", tmembercredit);
        setDict(model);
		return "tmembercreditView";
	}

	/**
	 * 会员信用信息表查询（分页）
	 * 
	 * @param tmembercredit
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmembercreditList.do")
	public String listPage(Tmembercredit tmembercredit, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tmembercreditService.getTmembercreditByPage(toMap(tmembercredit),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tmembercreditList";
	}

	/**
	 * 会员信用信息表查询
	 * 
	 * @param tmembercredit
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("member/v_tmembercreditList.jspx")
	public String list(HttpServletRequest request, ModelMap model) {
		Tmembercredit tmembercredit = new Tmembercredit();
		tmembercredit.setTmember((Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY));
		List<Tmembercredit> list = tmembercreditService.getTmembercredit(toMap(tmembercredit));
		if(list.size()<=0)
			list.add(tmembercredit);
		model.addAttribute("entity", list.get(0));
        setDict(model);
		return "/front/member/creditInfo.html";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tmembercredit
	 * @return
	 */	private Map<String,Object> toMap(Tmembercredit tmembercredit){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tmembercredit.getMcid()!=null&&!tmembercredit.getMcid().equals("")){
			map.put("mcid", tmembercredit.getMcid());
		}
		if(tmembercredit.getTmember()!=null){
			map.put("Tmember", tmembercredit.getTmember());
		}
		if(tmembercredit.getCreditscore()!=null&&!tmembercredit.getCreditscore().equals("")){
			map.put("creditscore", tmembercredit.getCreditscore());
		}
		if(tmembercredit.getCreditrating()!=null&&!tmembercredit.getCreditrating().equals("")){
			map.put("creditrating", tmembercredit.getCreditrating());
		}
		if(tmembercredit.getTotalcredits()!=null&&!tmembercredit.getTotalcredits().equals("")){
			map.put("totalcredits", tmembercredit.getTotalcredits());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tmembercredit tmembercredit, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TmembercreditService tmembercreditService;   
}